package com.zhongzhi.javafx.controller;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.ibatis.annotations.Select;

import com.zhongzhi.common.Common;
import com.zhongzhi.springboot.pojo.Equipment;
import com.zhongzhi.util.ClientUtil;
import com.zhongzhi.util.JsonResult;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

import javafx.scene.control.TextField;
import javafx.stage.Stage;
import lombok.extern.log4j.Log4j;
import javafx.scene.control.Label;

import javafx.scene.control.TextArea;

import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
@Log4j
public class StationNewViewController implements Initializable{
	@FXML
	private Label text1;

	@FXML
	private MenuButton EquipmentId;

	@FXML
	private TextField Stationid;

	@FXML
	private TextField numa;

	@FXML
	private TextField numb;

	@FXML
	private TextArea remarks;

	@FXML
	private Button stop;

	@FXML
	private Button add;

	@FXML
	private Label textts;
	
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		log.info("新增工位窗口:开始初始化");
		//设备查询
		Select();
		
		//按钮取消事件
		stop.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				Stage window = (Stage) stop.getScene().getWindow();
				window.close();
			}
		});
		//按钮确定事件
		add.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				if (Common.getPlcItemEquipment()==null) {
					textts.setText("没有选择任何设备");
				}else if (Stationid.getText()==null || Stationid.getText().equals("")) {
					textts.setText("工位名称为空");
				}else if (numa.getText().matches("-?[1-9]\\d*")==false) {
					textts.setText("通道a只能填写数字");
				}else if (numb.getText().matches("-?[1-9]\\d*")==false) {
					textts.setText("通道b只能填写数字");
				}else {
					Integer a=1;
					Integer b=2;
					try {
						a=Integer.valueOf(numa.getText());
						if (a>12 || a<0) {
							textts.setText("通道a取值范围1-12");
						}
						try {
							b=Integer.valueOf(numb.getText());
							if (b>12 || b<0) {
								textts.setText("通道b取值范围1-12");
							}
							insert(a,b);
						} catch (Exception e) {
							textts.setText("通道b只能填写数字");
						}
					} catch (Exception e) {
						textts.setText("通道a只能填写数字");
					}
				}
			}
		});
		if (remarks.getText()==null || remarks.getText().equals("")) {
			remarks.setText("无");
		}
		log.info("新增工位窗口:初始化完成");
	}

	//插入数据库
	private void insert(Integer a,Integer b) {
		try {
			String s = "http://127.0.0.1:8080/station/insert?id=1&equipmentId="+Common.getPlcItemEquipment().getId()
					+"&stationname=" + Stationid.getText().trim() 
					+"&numa=" + a
					+"&numb=" + b
					+"&remarks=" + remarks.getText().trim();
			log.info("新增工位窗口:存入数据库:"+s);
			String gethttp = ClientUtil.Gethttp(s);
			log.info("新增工位窗口:存入数据库结果:"+gethttp);
			JsonResult jsonobj = ClientUtil.Jsonobj(gethttp, JsonResult.class);
			if (jsonobj.getCode()==200) {
				ScrollPane sp = Common.getSp();
				sp.setContent(FXMLLoader.load(getClass().getResource("/fxml/run/Station.fxml")));
				Stage window = (Stage) stop.getScene().getWindow();
				window.close();
			}else {
				textts.setText(jsonobj.getMessage());
			}
		} catch (Exception e) {
			log.error("新增工位窗口:存入数据库失败:"+e);
		}
		
		
	}
	//设备查询
	private void Select() {
		try {
			String s = "http://127.0.0.1:8080/equipment/select?pages=1&line=255";
			log.info("新增工位窗口:设备查询:"+s);
			String gethttp = ClientUtil.Gethttp(s);
			log.info("新增工位窗口:设备查询结果:"+gethttp);
			JsonResult jsonobj = ClientUtil.Jsonobj(gethttp, JsonResult.class);
			if (jsonobj!=null) {
				String string = jsonobj.getData().toString();
				List<Equipment> eqlist = ClientUtil.Jsonlist(string,Equipment.class);
				for (Equipment equipment : eqlist) {
					MenuItem menuItem = new MenuItem(equipment.getPlcName());
					//增加事件
					menuItem.setOnAction(new EventHandler<ActionEvent>() {
						
						@Override
						public void handle(ActionEvent event) {
							EquipmentId.setText(equipment.getPlcName());
							Common.setPlcItemEquipment(equipment);
						}
					});
					
					EquipmentId.getItems().add(menuItem);
					
				}
			}
		} catch (Exception e) {
			log.error("新增工位窗口:设备查询失败"+e);
		}
		
		
		
	}

}
